import axios from 'axios'
import { Message } from 'element-ui'

const deviceRequest = axios.create({
  baseURL: '/shuzhongtianDeviceApi',
  // baseURL: process.env.VUE_APP_BASE_API,
  timeout: 30000
})

deviceRequest.interceptors.request.use(config => {
  return config
}, err => {
  return Promise.reject(err)
})

deviceRequest.interceptors.response.use(response => {
  if (response.data instanceof ArrayBuffer) { return response.data }
  // console.log(response)
  const res = response.data
  const { msg } = res
  if (res.success || response.status === 200) {
    return res
  } else {
    // 提示
    // Promise.reject
    Message({ type: 'error', msg })
    return Promise.reject(new Error(msg))
  }
}, err => {
  console.log(err.response.status)
  // 对于其他错误 ==> 简单消息提示
  return Promise.reject(err)
})

export default deviceRequest // 导出axios实例
